package com.woniuxy.tfss.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.tfss.entity.*;
import com.woniuxy.tfss.service.*;
import com.woniuxy.tfss.utils.MyUtils;
import com.woniuxy.tfss.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class Quartz {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private RiderService riderService;

    @Autowired
    private UdService udService;

    @Autowired
    private WebSocketServer webSocketServer;

    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void aitConfirmOrder() throws Exception {//未付款订单15min自动取消
        String now = MyUtils.getNow();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(now);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.add(Calendar.MINUTE, -15);
        List<Order> orderList=orderService.getWaitConfirmOrder();

        orderList.forEach(o->{

            try {
                if(df.parse(o.getOrderTime()).before(calendar.getTime())){
                    o.setStatus(Order.NOTPAYCANCEL);
                    orderService.updateById(o);
                    webSocketServer.sendInfo("u" + o.getUserId(), new Message<>(905, o));//发消息给用户
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });

    }

    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void waitRiderOrder() throws Exception {
        String now = MyUtils.getNow();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d = df.parse(now);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.add(Calendar.MINUTE, -30);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(d);
        calendar2.add(Calendar.MINUTE, 30);
        List<Order> orderList=orderService.getWaitRiderOrder();
        orderList.forEach(o->{
            try {
                if(df.parse(o.getArrangeTime()).before(calendar.getTime())){//预约订单无骑手接单,超过预约时间30min自动取消
                    o.setStatus(Order.NORIDERCANCEL);
                    orderService.updateById(o);
                    webSocketServer.sendInfo("u" + o.getUserId(), new Message<>(904, o));//发消息给用户
                }
                if(df.parse(o.getArrangeTime()).compareTo(calendar2.getTime())==0){//预约订单距预约时间30min提醒骑手开始抢单
                    List<Rider> riders=riderService.getWaitRiders();
                    riders.forEach(r->{
                        webSocketServer.sendInfo("r"+r.getId(),new Message<>(902,o));//群发消息给空闲骑手
                    });
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
    }

    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void ridingOrder() throws Exception {//配送超时订单补偿用户优惠券
        String now = MyUtils.getNow();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(now);
        Calendar calendar = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar.setTime(d);
        calendar2.setTime(d);
        List<Order> orderList=orderService.getRidingOrder();
        orderList.forEach(o->{
          //  calendar.add(Calendar.MINUTE, -(o.getExpectTime()));
            try {
                Calendar c = Calendar.getInstance();
                c.setTime(df.parse(o.getAcceptTime()));
                c.add(Calendar.MINUTE,o.getExpectTime());
                if(c.getTime().before(calendar.getTime())){
                    o.setStatus(Order.OVERTIME);//更改订单状态为超时
                    orderService.updateById(o);
                    webSocketServer.sendInfo("u" + o.getUserId(), new Message<>(906, o));//发消息给用户
                    webSocketServer.sendInfo("r" + o.getRiderId(), new Message<>(907, o));//发消息给骑手
                    userService.addDiscount(o.getUserId(),2,now);//补偿一张八折券(id=2)
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });

    }

    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void deleteOverTimeDiscounts() throws ParseException {//删除过期的优惠券
        String now = MyUtils.getNow();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(now);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        List<Ud> uds=udService.list();
        uds.forEach(ud -> {
            try {
                Date jzTime=df.parse(ud.getTime());
                if(jzTime.before(calendar.getTime())){
                    QueryWrapper<Ud> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("userid",ud.getUserId());
                    queryWrapper.eq("discountid",ud.getDiscountId());
                    queryWrapper.eq("time",ud.getTime());
                    udService.remove(queryWrapper);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        });

    }
}
